package com.wireless.action;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.util.ValueStack;
import com.wireless.base.BaseAction;
import com.wireless.entity.Menutbl;
import com.wireless.entity.Menutypetbl;
import com.wireless.entity.Usertbl;
import com.wireless.service.MenuService;
import com.wireless.service.UserService;

@Namespace("/menu")
@ParentPackage("json-default")
@Controller
public class MenuAction extends BaseAction {
	@Autowired
	private MenuService menuService;
	private Menutbl menu;
	private String code;
	private String menutype;
	private File file;
	private String fileFileName;
	private List<Menutypetbl> menutypelist;
	private List<Menutbl> menulist;
	private int delid;
	private int typeID;
	private int id;

	@Action(value = "addMenu", results = {
			@Result(name = "success", location = "/views/menu/menu.jsp"),
			@Result(name = "fail", location = "/views/menu/menuAdd.jsp") })
	public String addMenu() {

		Menutypetbl menuType = menuService.getMenuType(Integer
				.valueOf(menutype));
		Menutbl menu1 = new Menutbl();
		menu1.setMenutypetbl(menuType);
		menu1.setName(menu.getName());
		menu1.setPrice(menu.getPrice());
		menu1.setRemark(menu.getRemark());
		System.out.println(fileFileName);
		try {
			FileUtils.copyFile(file, new File(
					"/home/wwwroot/default/web/imgSource/images", fileFileName));
		} catch (IOException e) {
			e.printStackTrace();
		}
		menu1.setPic("http://www.tulaoda.top/web/imgSource/images/"+fileFileName);
		boolean flag = menuService.addMenu(menu1);
		if (flag == true) {
			code = "1";
			menulist = menuService.getMenuList();
			servletRequest.setAttribute("menuAdd", "yes");
			return SUCCESS;

		} else {
			code = "0";
			servletRequest.setAttribute("menuAdd", "no");
			return "fail";
		}

	}

	@Action(value = "menulist", results = { @Result(name = "success", location = "/views/menu/menu.jsp") })
	public String menulist() {
		menulist = menuService.getMenuList();
		return SUCCESS;
	}

	@Action(value = "menutypelist", results = { @Result(name = "success", location = "/views/menu/menuAdd.jsp") })
	public String menutypelist() {
		menutypelist = menuService.getMenuTypeList();
		return SUCCESS;
	}

	@Action(value = "delMenu", results = { @Result(name = "success", location = "/views/menu/menu.jsp") })
	public String delMenu() {
		menuService.delMenu(delid);
		menulist = menuService.getMenuList();
		return SUCCESS;
	}

	@Action(value = "getMenuByType", results = { @Result(name = "success", type = "json") })
	public String getMenuByType() {
		menulist = menuService.getMenuList();
		return SUCCESS;
	}

	@Action(value = "getType", results = { @Result(name = "success", type = "json") })
	public String getType() {
		menutypelist = menuService.getMenuTypeList();
		return SUCCESS;
	}

	@Action(value = "getGoodById", results = { @Result(name = "success", type = "json") })
	public String getGoodById() {
		menu = menuService.getMenu(id);
		return SUCCESS;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public Menutbl getMenu() {
		return menu;
	}

	public void setMenu(Menutbl menu) {
		this.menu = menu;
	}

	public String getMenutype() {
		return menutype;
	}

	public void setMenutype(String menutype) {
		this.menutype = menutype;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	public List<Menutypetbl> getMenutypelist() {
		return menutypelist;
	}

	public void setMenutypelist(List<Menutypetbl> menutypelist) {
		this.menutypelist = menutypelist;
	}

	public List<Menutbl> getMenulist() {
		return menulist;
	}

	public void setMenulist(List<Menutbl> menulist) {
		this.menulist = menulist;
	}

	public int getDelid() {
		return delid;
	}

	public void setDelid(int delid) {
		this.delid = delid;
	}

	public int getTypeID() {
		return typeID;
	}

	public void setTypeID(int typeID) {
		this.typeID = typeID;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

}
